import {ClientManager} from "./events/client";
import {objectToArray} from "../utils/arr";
import *  as lodash from 'lodash'

export enum ClientTypes {
    client = 'client',
    oasis_client = 'oasis_client',
}

// process.env.DEBUG = "socket.io:socket";
export class SocketManager {
    public readonly clients: any = {};
    public readonly io: any;
    public readonly socket: any;
    public readonly app: any;

    constructor(io: any) {
        this.io = io;
        this.app = io.app;
        this.socket = io.socket;
        io.on('connection', (context: any) => {
            const manager = new ClientManager(context, this);
            this.clients[manager.id] = manager;
        });
    }

    public removeClient(id) {
        this.clients[id] && delete this.clients[id]
    }


    /**
     * 获取账户信息
     * @param type
     */
    public getClient(type: ClientTypes = ClientTypes.oasis_client): ClientManager {
        const clients = this.getClientList().filter(c => {
            console.log(c.clientType === type);
            return c.clientType === type;
        });
        if (clients.length <= 0) return null;
        return lodash.sample(clients)
    }

    /**
     *获取客户端列表
     */
    public getClientList(): ClientManager[] {
        return objectToArray(this.clients);
    }


}
